<?php


/*
 * action class for  delete user
 * @author Chamara Bandara
 * @copyright Copyright &copy; 2011 Allion Technologies (Pvt) Ltd
 */

class DeleteAction extends CAction {

   public function run() {
      $controller = $this->getController();
      if (Yii::app()->request->isPostRequest) {
         $model = User::model()->loadModel($_GET['id']);
         // we only allow deletion via POST request
         if (is_file(Yii::app()->params['imageSavePath'] . $model->picture)) {
            unlink(Yii::app()->params['imageSavePath'] . $model->picture);
            if (is_dir(Yii::app()->params['imageSavePath'] . $_GET['id']))
               rmdir(Yii::app()->params['imageSavePath'] . $_GET['id'] . DIRECTORY_SEPARATOR . 'thumbnail');
            rmdir(Yii::app()->params['imageSavePath'] . $_GET['id']);
         }

         $model->delete();

         // if AJAX request (triggered by deletion via admin grid view), we should not redirect the browser
         if (!isset($_GET['ajax']))
            $this->redirect(isset($_POST['returnUrl']) ? $_POST['returnUrl'] : array('admin'));
      }
      else
         throw new CHttpException(400, 'Invalid request. Please do not repeat this request again.');
   }
}

